package com.mty.springboot.controller;

import com.mty.springboot.model.Loss;
import com.mty.springboot.model.Obtain;
import com.mty.springboot.model.PageInfo;
import com.mty.springboot.model.User;
import com.mty.springboot.service.LossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class LossController {

    // 依赖注入
    @Autowired
    private LossService lossService;
    @Value("${server.port}")
    private String port;
    @Value("${uploadDir}")
    private String uploadDir;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findLoss")
    public String findLoss(Integer pageIndex, Integer pageSize,String category,String remark, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("category",category);
        mp.put("remark",remark);
        mp.put("nosh","11");
        PageInfo<Loss> pageList = lossService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "LossList";
    }


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findNoPassLoss")
    public String findNoPassLoss(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("status","01");
        PageInfo<Loss> pageList = lossService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "NotLossList";
    }

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/MyfindLoss")
    public String MyfindLoss(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        User user = (User) session.getAttribute("ad");
        mp.put("uid",user.getId());
        mp.put("nosh","11");
        PageInfo<Loss> pageList = lossService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "MyLossList";
    }


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/MyPushLoss")
    public String MyPushLoss(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        User user = (User) session.getAttribute("ad");
        mp.put("pid",user.getId());
        mp.put("nosh","11");
        PageInfo<Loss> pageList = lossService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "MyPushLoss";
    }


    /**
     *
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findInitLoss")
    public String findInitLoss(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("status","11");
        PageInfo<Loss> pageList = lossService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "ValidateLossList";
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/addLoss" ,method = RequestMethod.POST)
    @ResponseBody
    public String addLoss( @RequestBody Loss loss,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "202";
        }
        User user = (User) session.getAttribute("ad");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM:dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        loss.setPtime(time);
        loss.setStatus("11");
        loss.setPid(user.getId());
        int d = lossService.addLoss(loss);
        return "ok";
    }


    /*** 删除*/
    @RequestMapping( "/deleteLoss")
    @ResponseBody
    public String deleteLoss(String id) {
        int d = lossService.deleteLoss(id);
        return "LossList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateLoss")
    public String updateLoss( Loss loss) {
        int d = lossService.updateLoss(loss);
        return "redirect:/findLoss";
    }

    /**
     * 文件上传
     * @param dropFile
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/avatar", method = RequestMethod.POST)
    public Map<String, Object> acticleAvatar(MultipartFile dropFile, HttpServletRequest request) throws IOException {
        Map<String, Object> result = new HashMap<>();
        //获取文件后缀
        String fileName = dropFile.getOriginalFilename();
        String fileSuffix = fileName.substring(fileName.lastIndexOf('.'));
        //文件存放路径
        String fileDirPath = new String(uploadDir);
        File fileDir = new File(fileDirPath);
        //判断文件是否存在
        if (!fileDir.exists()){
            fileDir.mkdirs();
        }
        File file = new File(fileDir.getAbsolutePath()+File.separator+UUID.randomUUID() + fileSuffix);
        try {
            dropFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //传到前端
        result.put("fileName", "http://localhost:"+port+"/upload/"+file.getName());
        return result;
    }

    /**
     * 更新状态
     */
    @RequestMapping( "/updateLossStatus")
    @ResponseBody
    public String updateLossStatus(String id, String status) {
        try{
            Loss loss = new Loss();
            loss.setId(id);
            if(status.equals("03")){
                loss.setStatus("00");
                loss.setUid("");
                loss.setContent("");
            }else{
                loss.setStatus(status);
            }
            int d = lossService.updateLoss(loss);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 申请归还
     */
    @RequestMapping( value = "/shenqing",method = RequestMethod.POST)
    @ResponseBody
    public String shenqing(@RequestBody Loss loss,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "202";
        }
        User user = (User) session.getAttribute("ad");
        loss.setUid(user.getId());
        loss.setStatus("01");
        int d = lossService.updateLoss(loss);
        return "ok";
    }

    /*** 按照用户选择的物品类型查询*/
    @RequestMapping( "/findLossById")
    public String findLossById(String id,Model model,HttpServletRequest request) {
        Loss loss= lossService.findLossById(id);
        model.addAttribute("loss",loss);
        return "LossEdit";
    }


    /*** 按照用户输入的物品特征关键词查询*/
    @RequestMapping( value = "/queryLossInfo",method = RequestMethod.POST)
    @ResponseBody
    public Loss queryObtainInfo(String id, HttpServletRequest request) {
        Loss loss = lossService.findLossById(id);
        return  loss;
    }
}
